package account.common;

import javax.validation.ValidationException;

public class RealName extends SafeInputText {

    public static final int MIN_REAL_NAME_LENGTH = 1;

    public static final int MAX_REAL_NAME_LENGTH = 100;

    /**
     * 将不安全的HTML文本转义成为纯文本，并检查字符串长度上下限
     *
     * @param unsafeInput 不安全的文本字符串
     * @throws ValidationException 当用户名字符串不满足长度上下限时抛出 ValidationException 校验异常
     * @return RealName 对象
     */
    public static RealName fromUnsafeText(String unsafeInput) throws ValidationException {
        RealName realName = new RealName();
        realName.input(unsafeInput);
        if (realName.length() > MAX_REAL_NAME_LENGTH) {
            String msg1 = String.format(
                    "The inputted name \"%s\" is too long! The maximum length is %d",
                    realName.asPlainText(), MAX_REAL_NAME_LENGTH);
            throw new ValidationException(msg1);
        }
        if (realName.length() < MIN_REAL_NAME_LENGTH) {
            String msg2 = String.format(
                    "The inputted name \"%s\" is too short! The minimum length is %d",
                    realName.asPlainText(), MIN_REAL_NAME_LENGTH);
            throw new ValidationException(msg2);
        }
        return realName;
    }

    protected RealName() {
        super();
    }
}
